<template>
    <div class="category-detail-page">
        <el-card shadow="never">
            <el-form
                :model="form"
                label-position="right"
                label-suffix=":"
                label-width="100px"
                ref="form"
                :rules="rules">
                <el-form-item label="服务名称" prop="name">
                    <el-input
                        v-model="form.name"
                        placeholder="请输入服务名称"
                        class="input-width"
                    ></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <Tinymce v-model="form.content"/>
                </el-form-item>
            </el-form>
            <div class="text-center">
                <el-button type="primary" @click="onSave">保 存</el-button>
                <el-button @click="onBack">返 回</el-button>
            </div>
        </el-card>
    </div>
</template>

<script>
import Tinymce from '@/components/base/Tinymce';

export default {
    components: {
        Tinymce,
    },
    data() {
        return {
            form: {
                content: '',
                name: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入服务名称', trigger: 'blur' },
                ],
                content: [
                    { required: true, message: '请输入内容', trigger: 'blur' },
                ],
            },
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (!valid) return;
                API.put('merit_additional_service', this.form).then(() => {
                    $ele.$message.success('编辑成功');
                    this.onBack();
                });
            });
        },

        getDetail() {
            this.loading = true;
            API.get(`merit_additional_service/${this.id}`).then(res => {
                this.form = res;
            }).finally(() => this.loading = false);
        },

        onBack() {
            this.$router.back();
        },
    },
    mounted() {
        this.id = this.$route.params.id;
        if (this.id) this.getDetail();
    },
};
</script>

<style lang="scss" scoped>
.input-width {
    max-width: 300px;
    width: 100%;
}
</style>
